<html>
<script>
function load() {
opera.postError('load() in index.html');		

	var m_exclude_sites;
	var m_block_sites;
	var m_bad_keywords;
	var m_strict_mode;
	var m_urlblacklist_mode;	
	
	opera.extension.onmessage = function(event)
	{
	opera.postError('event received in index.html - '+event.data.request);
		if (event.data.request == "ext_load")
		{
			var xhr = new XMLHttpRequest();
			xhr.onreadystatechange = function() {
			  if (xhr.readyState == 4) {			  
				if(xhr.responseText != "Unauthorized")
				{
					event.source.postMessage({"action": "ext_load","result": xhr.responseText});
				}
			  }
			}
			var url="http://antiporn.felsoft.com.au/rpc_access_p.php";
			var formData = "u="+localStorage["login"];
			formData = formData +"&p="+localStorage["password"];
			formData = formData +"&c="+localStorage["computer"];
			formData = formData +"&url="+encodeURIComponent(event.data.req_url);
			
			xhr.open("POST", url, true);
			xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhr.send(formData);
		}
		else if (event.data.request == "settings")
		{
		opera.postError('Inside [settings]');
			/// Settings will be called first for every page load... right place to log the url on server
			var xhrlog = new XMLHttpRequest();
			xhrlog.onreadystatechange = function() {}
			var urllog="http://antiporn.felsoft.com.au/rpc_log_p.php";
			
			var formData = "u="+localStorage["login"];
			formData = formData +"&p="+localStorage["password"];
			formData = formData +"&c="+localStorage["computer"];
			formData = formData +"&url="+encodeURIComponent(event.data.req_url);
			
			opera.postError('URL (logged) ='+event.data.req_url);
			xhrlog.open("POST", urllog, true);
			xhrlog.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			xhrlog.send(formData);

			
			
			if(m_exclude_sites == undefined)
			{		
				var xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function() {
				  if (xhr.readyState == 4) {
					if(xhr.responseText != 'Unauthorized')
					{
					m_exclude_sites=xhr.responseText.split('|')[0];
					m_block_sites=xhr.responseText.split('|')[1];
					m_bad_keywords=xhr.responseText.split('|')[2];
					m_strict_mode=xhr.responseText.split('|')[3];
					m_urlblacklist_mode=xhr.responseText.split('|')[4];
					

					if(m_urlblacklist_mode == '1')
					{
						var xhr_1 = new XMLHttpRequest();
						xhr_1.onreadystatechange = function() {
						  if (xhr_1.readyState == 4) {	  
							event.source.postMessage(
								{
									"action": "settings",
									"exclude_sites": m_exclude_sites,
									"block_sites": m_block_sites,
									"bad_keywords": m_bad_keywords,
									"strict_mode": m_strict_mode,
									"urlblacklisted": xhr_1.responseText,
								});
							}
						}
						var url="http://antiporn.felsoft.com.au/urlblacklist.php?url="+escape(event.data.req_url);
						xhr_1.open("GET", url, true);
						xhr_1.send();
					}
					else
					{
						event.source.postMessage(
						{
							"action": "settings",
							"exclude_sites": m_exclude_sites,
							"block_sites": m_block_sites,
							"bad_keywords": m_bad_keywords,
							"strict_mode": m_strict_mode,
							"urlblacklisted": "allow",
						});
					}

					}
				  }
				}
				var url="http://antiporn.felsoft.com.au/rpc_settings_p.php";
				var formData = "u="+localStorage["login"];
				formData = formData +"&p="+localStorage["password"];
				formData = formData +"&c="+localStorage["computer"];
				xhr.open("POST", url, true);
				xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
				xhr.send(formData);
			}
			else
			{
					if(m_urlblacklist_mode == '1')
					{
						var xhr_2 = new XMLHttpRequest();
						xhr_2.onreadystatechange = function() {
						  if (xhr_2.readyState == 4) {	
								event.source.postMessage(
								{
									"action": "settings",
									"exclude_sites": m_exclude_sites,
									"block_sites": m_block_sites,
									"bad_keywords": m_bad_keywords,
									"strict_mode": m_strict_mode,
									"urlblacklisted": xhr_2.responseText,
								});						  
							}
						}
						var url="http://antiporn.felsoft.com.au/urlblacklist.php?url="+escape(event.data.req_url);
						xhr_2.open("GET", url, true);
						xhr_2.send();
					}
					else
					{
						event.source.postMessage(
						{
							"action": "settings",
							"exclude_sites": m_exclude_sites,
							"block_sites": m_block_sites,
							"bad_keywords": m_bad_keywords,
							"strict_mode": m_strict_mode,
							"urlblacklisted": "allow",
						});	
					}
			}
		}		
	};

}

</script>
 <body onload="load()">
 </body>
</html>
